/*`<div class="page">
    <div class="prevBox">
        <span class="first">|&lt;</span>
        <span class="prev">&lt;</span>
    </div>
    <div class="pages">
        <a href="" class="curr">1</a><a href="">2</a><a href="">3</a><a href="">4</a><a href="">5</a>
    </div>
    <div class="nextBox">
        <a href="" class="next">&gt;</a>
        <a href="" class="last">&gt;|</a>
    </div>
    <div class="cnt">当前/总共：1/10</div>
</div>`;
*/
var qs=require('querystring');

class Page{
    constructor({url='/',page={total:1,curr:1},query={}}={}){
        this.url=url;
        this.page=page;
        this.queryStr=qs.stringify(query);
        this.queryStr=this.queryStr?'&'+this.queryStr:'';
        this.html='';
        console.log('page this:',this);
        this.init();
    }
    path(page=1){
        return `${this.url}?page=${page}${this.queryStr}`;
    }
    preBox(){
        var s;
        if(1>=this.page.curr){
            s='<span class="first">|&lt;</span><span class="prev">&lt;</span>';
        }else{
            s=`<a href="${this.path(1)}" class="first">|&lt;</a><a href="${this.path(this.page.prev)}" class="prev">&lt;</a>`;
        }
        return s;
    }
    nextBox(){
        var s;
        if(this.page.curr>=this.page.total){
            s='<span class="next">&gt;</span><span class="last">&gt;|</span>';
        }else{
            s=`<a href="${this.path(this.page.next)}" class="next">&gt;</a><a href="${this.path(this.page.total)}" class="last">&gt;|</a>`;
        }
        return s;
    }
    pages(){
        var s='',i=0;
        while(i++<this.page.total){
            s+=`<a href="${this.url}?page=${i}${this.queryStr}" class="${(i==this.page.curr)?'curr':''}">${i}</a>`;
        }
        return s;
    }
    init(){
        this.html+='<div class=page>';
        if(this.page.total>=1){
            this.html+=`<div class=prevBox>${this.preBox()}</div>`;
            this.html+=`<div class=pages>${this.pages()}</div>`;
            this.html+=`<div class=nextBox>${this.nextBox()}</div>`;
            this.html+=`<div class=cnt>当前/总共：${this.page.curr}/${this.page.total}</div>`;
        }
        this.html+='</div>';
    }
    toString(){
        return this.html;
    }
}

module.exports=Page;